package org.gameg.service.biz.impl;

import io.swagger.annotations.ApiModelProperty;
import io.swagger.models.auth.In;
import org.gameg.bean.BetPower;
import org.gameg.bean.Game;
import org.gameg.bean.Player;
import org.gameg.bean.Power;
import org.gameg.mapper.biz.TGameMapper;
import org.gameg.model.biz.TGame;
import org.gameg.model.biz.TGamePlayer;
import org.gameg.service.biz.GameModelMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.ibase4j.core.base.BaseService;
import top.ibase4j.core.exception.BusinessException;
import top.ibase4j.core.util.InstanceUtil;

import java.math.BigDecimal;
import java.util.*;

/**
 * 猜大小单双合模式
 */
@Component
public class GuessSize extends BaseService<TGame, TGameMapper> implements GameModelMethod {

    @ApiModelProperty("猜大")
    public static final String BIGMETHOD = "17";
    @ApiModelProperty("猜小")
    public static final String TINYMETHOD = "18";
    @ApiModelProperty("猜单")
    public static final String SINGLEMETHOD = "19";
    @ApiModelProperty("猜双")
    public static final String DOUBLEMETHOD = "20";
    @ApiModelProperty("猜合")
    public static final String HEMETHOD = "21";

    //======================================================
    @ApiModelProperty("猜大单")
    public static final String BIGSINGLEMETHOD = "22";
    @ApiModelProperty("猜小单")
    public static final String TINYSINGLEMETHOD = "23";
    @ApiModelProperty("猜大双")
    public static final String BIGDOUBLEMETHOD = "24";
    @ApiModelProperty("猜小双")
    public static final String TINYDOUBLEMETHOD = "25";


    @Autowired
    private GameServiceImpl gameService;

    private static void initSize(List<Player> players, char type){
        for (Player player :
                players) {
            String bet = player.getBetStr();
            char[] betStr = bet.toCharArray();
            boolean sizeModel = false ;//默认为 不是猜大小单双合
            int i = 0 ;
            char[] betList = null;
            for (char betChar :
                    betStr) {
                if (betChar == type) {
                    betList = new char[betStr.length-1];
                    sizeModel = true;
                    player.setPlayerGameType("3");
                    if (i > 0 && i<(betStr.length)) {//不是第一个元素,拷贝前边的数据。
                        System.arraycopy(betStr,0,betList,0,i); //指定元素之前的数据
                    } else if (i == 0) { //第一个元素
                        System.arraycopy(betStr,i+1,betList,0,betStr.length-1); //指定元素之后的数据
                    }
                    betStr = betList;
                    if (type == '大') {
                        player.setGuessSize(GuessSize.BIGMETHOD);
                    }
                    if (type == '小') {
                        player.setGuessSize(GuessSize.TINYMETHOD);
                    }
                    if (type == '单') {
                        player.setGuessSize(GuessSize.SINGLEMETHOD);
                    }
                    if (type == '双') {
                        player.setGuessSize(GuessSize.DOUBLEMETHOD);
                    }
                    if (type == '合') {
                        player.setGuessSize(GuessSize.HEMETHOD);
                    }
                    bet = String.valueOf(betStr);
                    player.setSizeModel(sizeModel);
                    player.setBet(Integer.valueOf(bet.trim()));
                    player.setBetStr(bet.trim());
                }
                i++;
            }
            bet = String.valueOf(betStr);
            /*if (!sizeModel) {
                player.setBet(Integer.valueOf(bet));
            }*/
            System.out.println("押注："+bet+",是否为大小单双和模式:"+sizeModel);
        }
    }


    private static void initSize (List<Player> players, String type){
        for (Player player :
                players) {
            String bet = player.getBetStr();
            char[] betStr = bet.toCharArray();
            boolean sizeModel = false ;//默认为 不是猜大小单双合
            char[] betList = null;
            for (int i = 0; i <betStr.length-1 ; i++) {
                char [] guessStr = {betStr[i],betStr[i+1]};
                String typeStr = String.valueOf(guessStr) ;
                if (typeStr.equals(type)) {
                    betList = new char[betStr.length-2];
                    sizeModel = true;
                    player.setPlayerGameType("3");
                    if (i > 0 && i<(betStr.length-1)) {//不是第一个元素,拷贝前边的数据。
                        System.arraycopy(betStr,0,betList,0,i); //指定元素之前的数据
                    } else if (i == 0) { //第一个元素
                        System.arraycopy(betStr,i+2,betList,0,betStr.length-2); //指定元素之后的数据
                    }
                    betStr = betList;
                    if ("大单".equals(type)) {
                        player.setGuessSize(GuessSize.BIGSINGLEMETHOD);
                    }else if ("小单".equals(type)) {
                        player.setGuessSize(GuessSize.TINYSINGLEMETHOD);
                    }else if ("大双".equals(type)) {
                        player.setGuessSize(GuessSize.BIGDOUBLEMETHOD);
                    }else if ("小双".equals(type)) {
                        player.setGuessSize(GuessSize.TINYDOUBLEMETHOD);
                    }
                    bet = String.valueOf(betStr);
                    player.setSizeModel(sizeModel);
                    player.setBet(Integer.valueOf(bet.trim()));
                    player.setBetStr(bet.trim());
                }
            }
            bet = String.valueOf(betStr);
            /*if (!sizeModel) {
                player.setBet(Integer.valueOf(bet));
            }*/
            System.out.println("押注："+bet+",是否为大小单双和模式:"+sizeModel);
        }
    }

    public static void initSize(List<Player> players) {
        initSize(players, "大单");
        initSize(players, "小单");
        initSize(players, "大双");
        initSize(players, "小双");
        initBaccarat(players,"庄对");
        initBaccarat(players,"闲对");
        initSize(players, '大');
        initSize(players, '小');
        initSize(players, '单');
        initSize(players, '双');
        initSize(players, '合');
        initBaccarat(players,'庄');
        initBaccarat(players,'闲');
        initBaccarat(players,'和');
        for (Player p :
                players) {
            /*//判断玩家玩的模式
            if (p.isSizeModel()) {//大小单双和
                p.setPlayerGameType("3");
            }*/
            if (!p.getPlayerGameType().equals("4")) {
                if (p.getBet() == null || p.getBet().equals("")) {
                    p.setBet(Integer.valueOf(p.getBetStr()));
                }
            }
        }
    }

    public static void initBetPower(List<Player> players){
        for (Player player :
                players) {
            String betStr = player.getBetStr();
            List<BetPower> betPowerList = new ArrayList<>();
            //逻辑：判断是否带有买字，有则属于特码 ，给玩家标识特码游戏模式，
            int intIndex = betStr.indexOf("买");
            String playerGameType = "";
            if (intIndex == -1) {//不是特码
                System.out.println("不是特码模式");
                continue;
            } else {//是特码
                System.out.println("是特码模式");
                playerGameType = "4";
            }
            //特码格式输入字符串解析：
            String[] betStrList = betStr.split("，|,");//获得所有买的码格式 。分别解析
            Integer playerBet = 0;
            for (int i = 0; i < betStrList.length; i++) {
                String[] betMap = betStrList[i].split("买");//根据买截取字符串。
                Integer bet = Integer.valueOf(betMap[1]);
                char [] betTypes = betMap[0].toCharArray();
                for (char betType :
                        betTypes) {
                    BetPower betPower = new BetPower();
                    betPower.setBet(bet);
                    if (betType == '0') {
                        betPower.setBetPoint(10+25);
                    } else {
                        betPower.setBetPoint(Integer.valueOf(String.valueOf(betType))+25);
                    }
                    betPowerList.add(betPower);
                    System.out.println("牛"+betType+":"+bet);
                    playerBet += bet;
                }
            }
            player.setBetPowerList(betPowerList);
            player.setPlayerGameType("4");
            player.setBet(playerBet);
        }
    }

    private static void initBaccarat(List<Player> players, char type){
        for (Player player :
                players) {
            String bet = player.getBetStr();
            char[] betStr = bet.toCharArray();
            int i = 0 ;
            char[] betList = null;
            for (char betChar :
                    betStr) {
                if (betChar == type && (player.getBetPoint()==null||player.getBetPoint().equals(""))) {
                    betList = new char[betStr.length-1];
                    player.setPlayerGameType("2");
                    if (i > 0 && i<(betStr.length)) {//不是第一个元素,拷贝前边的数据。
                        System.arraycopy(betStr,0,betList,0,i); //指定元素之前的数据
                    } else if (i == 0) { //第一个元素
                        System.arraycopy(betStr,i+1,betList,0,betStr.length-1); //指定元素之后的数据
                    }
                    betStr = betList;
                    if (type == '庄') {
                        player.setBetPoint(36);
                    }
                    if (type == '闲') {
                        player.setBetPoint(37);
                    }
                    if (type == '和') {
                        player.setBetPoint(38);
                    }
                    bet = String.valueOf(betStr);
                    player.setBet(Integer.valueOf(bet.trim()));
                }
                i++;
            }
            bet = String.valueOf(betStr);
        }
    }

    private static void initBaccarat (List<Player> players, String type){
        for (Player player :
                players) {
            String bet = player.getBetStr();
            char[] betStr = bet.toCharArray();
            char[] betList = null;
            for (int i = 0; i <betStr.length-1 ; i++) {
                char [] guessStr = {betStr[i],betStr[i+1]};
                String typeStr = String.valueOf(guessStr) ;
                if (typeStr.equals(type)) {
                    betList = new char[betStr.length-2];
                    player.setPlayerGameType("2");
                    if (i > 0 && i<(betStr.length-1)) {//不是第一个元素,拷贝前边的数据。
                        System.arraycopy(betStr,0,betList,0,i); //指定元素之前的数据
                    } else if (i == 0) { //第一个元素
                        System.arraycopy(betStr,i+2,betList,0,betStr.length-2); //指定元素之后的数据
                    }
                    betStr = betList;
                    if ("庄对".equals(type)) {
                        player.setBetPoint(39);
                    }else if ("闲对".equals(type)) {
                        player.setBetPoint(40);
                    }
                    bet = String.valueOf(betStr);
                    player.setBet(Integer.valueOf(bet.trim()));
                }
            }
            bet = String.valueOf(betStr);
        }
    }



    public static void main(String[] args) {
        BigDecimal bigDecimal = BigDecimal.valueOf(12.22);
        System.out.println(bigDecimal);
    }



    /**
     * 实现积分模式和喝水模式。
     * 本局已经默认为庄家了，猜大小单双和只有 有庄模式，没有无庄模式。
     * @param resultList
     * @param game
     */
    private void integralModel(List<TGamePlayer> resultList, Game game) {
        boolean isUpdate =true;
        String gameRule =game.getGameRule();//  游戏规则
        Player zh = null;
        List<Player> xians = InstanceUtil.newArrayList();
        Map<Integer, Power> powers = InstanceUtil.newHashMap();
        // powers = {"1":"大","2":"小","3":"单","4":"双","5":"合", "3.1":"大单","3.2":"小单","4.1":"大双", "4.2":"小双"}
        for (Power power : game.getPowers()) {
            if (power.getIsOpen() == null) {
                power.setIsOpen(false);   // 设置是否特殊排序，默认为不开启特殊排序
            }
            powers.put(Integer.valueOf(power.getGuessSize()), power);
        }
        boolean mainTimeout = false;     //  默认为没有超时
        TGame tGame = InstanceUtil.to(game, TGame.class);
        tGame = update(tGame);
        List<Player> qbs = InstanceUtil.newArrayList();  //抢错包玩家
        List<Player> players = InstanceUtil.newArrayList();//押注玩家
        List<Player> playerList = InstanceUtil.newArrayList();
        List<Player> winXPlayerList = InstanceUtil.newArrayList();// 获胜玩家闲家容器
        List<Player> loserXPlayerList = InstanceUtil.newArrayList(); //失败玩家闲家容器
        initPlayers(game,qbs,powers,tGame,resultList,players,isUpdate,zh,playerList);
        Collections.sort(qbs, new Comparator<Player>() {
            @Override
            public int compare(Player o1, Player o2) {
                return o1.getTime() > o2.getTime() ? 1 : o1.getTime() < o2.getTime() ? -1 : 0;
            }
        });
        Collections.sort(playerList, new Comparator<Player>() {
            @Override
            public int compare(Player o1, Player o2) {
                return o1.getTime() > o2.getTime() ? -1 : o1.getTime() < o2.getTime() ? 1 : 0;
            }
        });
        Collections.sort(players, new Comparator<Player>() {
            @Override
            public int compare(Player o1, Player o2) {
                return o1.getTime() > o2.getTime() ? 1 : o1.getTime() < o2.getTime() ? -1 : 0;
            }
        });
        //庄家没有头包那么一说。
        //boolean isZhFirst = "1".equals(gameRule) && "Y".equals(players.get(0).getIsZj());  //庄家
        Collections.sort(players, new Comparator<Player>() {
            @Override
            public int compare(Player o1, Player o2) {
                return o1.getBetNo() > o2.getBetNo() ? 1 : o1.getBetNo() < o2.getBetNo() ? -1 : 0;
            }
        });
        int zhScore = 0;
        noHavBInit(game, players, powers, isUpdate, playerList, qbs, zhScore, xians, zh, tGame, resultList);
        if ("1".equals(gameRule) && zh == null) {
            throw new BusinessException("缺少庄家");
        }
        mainTimeout = zh.getTime() >= game.getMainTime();//  获取是否超时

        String gameType = game.getGameType(); // 游戏类型

    }


    /**
     * 抢包处理：把所有抢包的玩家放在qbs 容器中，
     * 把所有抢包玩家放在 players 容器中 。
     * 把所有押注玩家放在 playerList 容器中。
     * @param game
     * @param qbs
     * @param powers
     * @param tGame
     * @param resultList
     * @param players
     * @param isUpdate
     * @param zh
     * @param playerList
     */
    private  void initPlayers(Game game ,List<Player> qbs,Map<Integer, Power> powers,TGame tGame,List<TGamePlayer> resultList,
                      List<Player> players,boolean isUpdate,Player zh,List<Player> playerList){
        GameServiceImpl gameServiceimpl = new GameServiceImpl();
        String gameRule =game.getGameRule();//  游戏规则
        for (Player player : game.getPlayers()) {
            if ("1".equals(gameRule) && !"Y".equals(player.getIsZj())) {//有庄，为闲用户
                if ((player.getBet() == null || player.getBet() == 0) && player.getAmount() != null
                        && player.getAmount().doubleValue() > 0 && player.getTime() != null && player.getTime() > 0) {//没有押注但是抢红包了
                    if (game.getPunishScore() != null && game.getPunishScore() > 0) {
                        player.setPoint(PointHelper.getPoint(game, powers, player.getAmount()));
                        player.setPower(powers.get(player.getPoint()).getPower());
                        player.setNiuPoint(powers.get(player.getPoint()).getNiuPoint());
                        if (player.getTime() < game.getPlayerTime()) { // 抢错包处理
                            gameServiceimpl.updatePlayerScore(tGame.getId(), player, resultList, -game.getPunishScore(), isUpdate);
                        } else {
                            gameServiceimpl.updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                        }
                    }
                    qbs.add(player);
                } else {
                    players.add(player);
                }
            } else if ("1".equals(gameRule) && "Y".equals(player.getIsZj())) {//有庄，为庄用户
                zh = player;
                if ("2".equals(game.getOneFen()) && zh.getAmount().doubleValue() == 0.01) {
                    throw new RuntimeException("当局游戏不算");
                }
                players.add(player);
            }
            if (player.getAmount() != null && player.getAmount().doubleValue() > 0 && player.getTime() != null
                    && player.getTime() > 0) {
                playerList.add(player);
            }
        }
    }

    /**
     * 无包处理 ： 目前保持和牛牛无包处理相似，无包 后认尾包算不算 数，待确定。
     * @param game
     * @param players
     * @param powers
     * @param isUpdate
     * @param playerList
     * @param qbs
     * @param zhScore
     * @param xians
     * @param zh
     * @param tGame
     * @param resultList
     */
    private void noHavBInit(Game game,List<Player> players,Map<Integer, Power> powers,boolean isUpdate,List<Player> playerList,List<Player> qbs,int zhScore,
                            List<Player> xians,Player zh,TGame tGame,List<TGamePlayer> resultList){
        String gameRule = game.getGameRule(); int q= 0;
        gameService = new GameServiceImpl();
        for (int i = 0; i < players.size(); i++) { //遍历押注玩家
            Player player = players.get(i);
            if (player.getBet() != null && player.getBet() > 0) {
                // 无包处理
                if (player.getAmount() == null || player.getAmount().doubleValue() == 0) {
                    if ("1".equals(gameRule)) { // 闲家无包
                        player.setProxy(!(qbs.get(q++).getTime() <= game.getPlayerTime()));
                        if ("Y".equals(player.getIsExe())) {
                            player.setTime(playerList.get(0).getTime());
                            player.setAmount(playerList.get(0).getAmount());
                            xians.add(player);
                        } else {// 这个待处理
                            switch (game.getPlayerNone()) {
                                case "1":
                                    player.setTime(playerList.get(0).getTime());
                                    player.setAmount(playerList.get(0).getAmount());
                                    xians.add(player);
                                    break;
                                case "2":
                                    player.setTime(playerList.get(1).getTime());
                                    player.setAmount(playerList.get(1).getAmount());
                                    xians.add(player);
                                    break;
                                case "3":
                                case "4":
                                    player.setPoint(PointHelper.getPoint(game, powers, player.getAmount()));
                                    player.setNiuPoint(powers.get(player.getPoint()).getNiuPoint());
                                    player.setPower(powers.get(player.getPoint()).getPower());
                                    if ("3".equals(game.getMainFirst())) {
                                        if ("3".equals(game.getPlayerNone())) {
                                            logger.warn("闲家{}无包全陪，庄家{}赢", player.getWxId(), zh.getWxId());
                                            player.setPower(zh.getPower());
                                            double score=0;
                                            if (player.isPoker()) {//该闲家是否梭哈
                                                score = player.getBet();
                                                player.setPower(1.0);
                                            } else if (!player.isPoker()) {
                                                score = player.getBet() * zh.getPower();
                                            }
                                            //int score = player.getBet() * zh.getPower();
                                            zh.setOrgCreditScore((int)(zh.getOrgCreditScore()+score));
                                            gameService.updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                                        } else if ("4".equals(game.getPlayerNone())) {
                                            logger.warn("闲家{}无包平赔，庄家{}赢", player.getWxId(), zh.getWxId());
                                            player.setPower(1.0);
                                            int score=0;
                                            if (player.isPoker()) {//该闲家是否梭哈
                                                score = player.getBet();
                                                player.setPower(1.0);
                                            } else if (!player.isPoker()) {
                                                score = player.getBet();
                                            }
                                            //int score = player.getBet();
                                            zh.setOrgCreditScore(zh.getOrgCreditScore()+score);
                                            gameService.updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
                                        }
                                    } else {
                                        xians.add(player);
                                    }
                                    continue;
                            }
                        }
                    }
                } else if ("1".equals(gameRule) && !"Y".equals(player.getIsZj())) {
                    xians.add(player);
                }
                player.setPoint(PointHelper.getPoint(game, powers, player.getAmount()));
                player.setNiuPoint(powers.get(player.getPoint()).getNiuPoint());
                player.setPower(powers.get(player.getPoint()).getPower());
            }
        }
    }


    @Override
    public void play(Game game, List<Player> xians, Player zh, TGame tGame, List<TGamePlayer> resultList, boolean isUpdate, boolean isZhFirst, int zhScore, List<Player> winXPlayerList, boolean mainTimeout, List<Player> loserXPlayerList, Map<Integer, Power> powers) {
        for (Player player : xians) {
            if ("2".equals(game.getOneFen()) && player.getAmount().doubleValue() == 0.01) {
                logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                gameService.updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
                continue;
            }
            if (isZhFirst && "1".equals(game.getMainFirst())) {
                logger.warn("闲家{}赢，庄家{}头包全赔", player.getWxId(), zh.getWxId());
                double score = 0;
                if (player.isPoker()) {//该闲家梭哈，只赔 百分比
                    score = (int)(player.getBet() * Double.valueOf("0." + game.getPoker()));
                } else if (!player.isPoker()) {
                    score = player.getBet() * player.getPower();
                }
                //int score = player.getBet() * player.getPower();
                zhScore -= score;
                player.setXianWinScore((int)score);
                winXPlayerList.add(player);
                //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
            } else if (isZhFirst && "2".equals(game.getMainFirst())) {
                logger.warn("闲家{}赢，庄家{}头包平赔", player.getWxId(), zh.getWxId());
                int score = 0;
                if (player.isPoker()) {//该闲家梭哈，只赔 百分比
                    score = (int)(player.getBet() * Double.valueOf("0." + game.getPoker()));
                } else if (!player.isPoker()) {
                    score = player.getBet();
                }
                //int score = player.getBet();
                zhScore -= score;
                player.setXianWinScore(score);
                winXPlayerList.add(player);
                //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
            } else {
                // 超时处理
                if (player.getTime() >= game.getPlayerTime() && "N".equals(player.getIsExe())
                        && !player.isProxy() && mainTimeout && "N".equals(zh.getIsExe()) && !zh.isProxy()) {
                    logger.warn("闲家{}和庄家{}都超时", player.getWxId(), zh.getWxId());
                    if ("1".equals(game.getDoubleTimeout())) {
                        logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                        gameService.updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
                    } else if ("2".equals(game.getDoubleTimeout())) {
                        logger.warn("闲家{}输，庄家{}赢", player.getWxId(), zh.getWxId());
                        player.setPower(zh.getPower());
                        double score = 0;
                        if (player.isPoker()) {
                            score = player.getBet();
                            player.setPower(1.0);
                        } else if (!player.isPoker()) {
                            score = player.getBet() * zh.getPower();
                        }
                        //int score = player.getBet() * zh.getPower();
                        zhScore+=score;
                        player.setXianWinScore((int)score);
                        loserXPlayerList.add(player);
                        gameService.updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                    } else if ("3".equals(game.getDoubleTimeout())) {
                        logger.warn("闲家{}平赔，庄家{}赢", player.getWxId(), zh.getWxId());
                        player.setPower(1.0);
                        int score = 0;
                        if (player.isPoker()) {
                            score = player.getBet();
                            player.setPower(1.0);
                        } else if (!player.isPoker()) {
                            score = player.getBet();
                        }
                        //int score = player.getBet() * 1;
                        zhScore+=score;
                        player.setXianWinScore(score);
                        loserXPlayerList.add(player);
                        gameService.updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
                    }
                    continue;
                } else if (mainTimeout && "N".equals(zh.getIsExe()) && !zh.isProxy()) {
                    logger.warn("庄家{}超时", zh.getWxId());
                    if ("1".equals(game.getMainTimeout())) {
                        logger.warn("闲家{}赢，庄家{}全赔", player.getWxId(), zh.getWxId());
                        double score = 0;
                        if (player.isPoker()) {
                            score = player.getBet();
                            player.setPower(1.0);
                        } else if (!player.isPoker()) {
                            score = player.getBet() * player.getPower();
                        }
                        //int score = player.getBet() * player.getPower();
                        zhScore -= score;
                        player.setXianWinScore((int)score);
                        winXPlayerList.add(player);
                        //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                    } else if ("2".equals(game.getMainTimeout())) {
                        logger.warn("闲家{}赢，庄家{}平赔", player.getWxId(), zh.getWxId());
                        int score = 0;
                        if (player.isPoker()) {
                            score = player.getBet();
                            player.setPower(1.0);
                        } else if (!player.isPoker()) {
                            score =player.getBet();
                        }
                        //int score = player.getBet();
                        zhScore -= score;
                        player.setXianWinScore(score);
                        winXPlayerList.add(player);;
                        //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                    } else if ("3".equals(game.getMainTimeout())) {
                        logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                        gameService.updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
                    } else if ("5".equals(game.getMainTimeout())) {
                        int result = PointHelper.compare(game, zh, player);
                        if (result == 1) {
                            logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                            gameService.updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
                        } else {
                            logger.warn("闲家{}赢，庄家{}输", player.getWxId(), zh.getWxId());
                            double score = 0;
                            if (player.isPoker()) {
                                player.setPower(1.0);
                                score = player.getBet();
                            } else if (!player.isPoker()) {
                                score =player.getBet() * player.getPower();
                            }
                            //int score = player.getBet() * player.getPower();
                            zhScore -= score;
                            player.setXianWinScore((int)score);
                            winXPlayerList.add(player);
                            //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                        }
                    }
                    if (!"4".equals(game.getMainTimeout())) {
                        continue;
                    }
                } else if (player.getTime() >= game.getPlayerTime() && "N".equals(player.getIsExe())
                        && !player.isProxy()) {
                    logger.warn("闲家{}超时", player.getWxId());
                    if ("1".equals(game.getPlayerTimeout())) {
                        logger.warn("闲家{}全赔，庄家{}赢", player.getWxId(), zh.getWxId());
                        player.setPower(zh.getPower());
                        double score = 0;
                        if (player.isPoker()) {
                            player.setPower(1.0);
                            score = player.getBet();
                        } else if (!player.isPoker()) {
                            score = player.getBet() * zh.getPower();
                        }
                        //int score = player.getBet() * zh.getPower();
                        zhScore+=score;
                        player.setXianWinScore((int)score);
                        loserXPlayerList.add(player);
                        gameService.updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                    } else if ("2".equals(game.getPlayerTimeout())) {
                        logger.warn("闲家{}平赔，庄家{}赢", player.getWxId(), zh.getWxId());
                        player.setPower(1.0);
                        int score = 0;
                        if (player.isPoker()) {
                            player.setPower(1.0);
                            score = player.getBet();
                        } else if (!player.isPoker()) {
                            score = player.getBet();
                        }
                        //int score = player.getBet();
                        zhScore+=score;
                        player.setXianWinScore(score);
                        loserXPlayerList.add(player);
                        gameService.updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
                    } else if ("3".equals(game.getPlayerTimeout())) {
                        logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                        gameService.updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                    } else if ("5".equals(game.getPlayerTimeout())) {
                        int result = PointHelper.compare(game, zh, player);
                        if (result == -1) {
                            logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                            gameService.updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                        } else {
                            logger.warn("闲家{}输，庄家{}赢", player.getWxId(), zh.getWxId());
                            player.setPower(zh.getPower());
                            double score = 0;
                            if (player.isPoker()) {
                                player.setPower(1.0);
                                score = player.getBet();
                            } else if (!player.isPoker()) {
                                score = player.getBet() * zh.getPower();
                            }
                            //int score = player.getBet() * zh.getPower();
                            zhScore+=score;
                            player.setXianWinScore((int)score);
                            loserXPlayerList.add(player);
                            gameService.updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                        }
                    }
                    if (!"4".equals(game.getPlayerTimeout())) {
                        continue;
                    }
                }else if (player.isProxy()&&"N".equals(player.getIsExe())) {
                    logger.warn("闲家{}超时", player.getWxId());
                    if ("1".equals(game.getPlayerTimeout())) {
                        logger.warn("闲家{}全赔，庄家{}赢", player.getWxId(), zh.getWxId());
                        player.setPower(zh.getPower());
                        double score = 0;
                        if (player.isPoker()) {
                            player.setPower(1.0);
                            score = player.getBet();
                        } else if (!player.isPoker()) {
                            score =  player.getBet() * zh.getPower();
                        }
                        //int score = player.getBet() * zh.getPower();
                        zhScore += score;
                        gameService.updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                    } else if ("2".equals(game.getPlayerTimeout())) {
                        logger.warn("闲家{}平赔，庄家{}赢", player.getWxId(), zh.getWxId());
                        player.setPower(1.0);
                        int score = 0;
                        if (player.isPoker()) {
                            player.setPower(1.0);
                            score = player.getBet();
                        } else if (!player.isPoker()) {
                            score = player.getBet();
                        }
                        //int score = player.getBet();
                        zhScore += score;
                        gameService.updatePlayerScore(tGame.getId(), player, resultList, -score, isUpdate);
                    } else if ("3".equals(game.getPlayerTimeout())) {
                        logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                        gameService.updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                    } else if ("5".equals(game.getPlayerTimeout())) {
                        int result = PointHelper.compare(game, zh, player);
                        if (result == -1) {
                            logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                            gameService.updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                        } else {
                            logger.warn("闲家{}输，庄家{}赢", player.getWxId(), zh.getWxId());
                            player.setPower(zh.getPower());
                            double score = 0;
                            if (player.isPoker()) {
                                player.setPower(1.0);
                                score = player.getBet();
                            } else if (!player.isPoker()) {
                                score = player.getBet() * zh.getPower();
                            }
                            // int score = player.getBet() * zh.getPower();
                            zhScore += score;
                            gameService.updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                        }
                    }
                    if (!"4".equals(game.getPlayerTimeout())) {
                        continue;
                    }
                }
                if (zh.getPoint() == player.getPoint()
                        && "Y".equals(powers.get(player.getPoint()).getIsDogfall())) {
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    gameService.updatePlayerScore(tGame.getId(), player, resultList, 0, isUpdate);
                    continue;
                }
                // 正常情况
                int result = PointHelper.compare(game,player);
                if (result == 1) { // 庄赢
                    logger.warn("闲家{}输，庄家{}赢", player.getWxId(), zh.getWxId());
                    player.setPower(zh.getPower());
                    double score = 0;
                    if (player.isPoker()) {
                        player.setPower(1.0);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        score = player.getBet() * zh.getPower();
                    }
                    //int score = player.getBet() * zh.getPower();
                    zhScore+=score;
                    player.setXianWinScore((int)score);
                    loserXPlayerList.add(player);
                    gameService.updatePlayerScore(tGame.getId(), player, resultList, (int)-score, isUpdate);
                } else if (result == -1) { // 闲赢
                    logger.warn("闲家{}赢，庄家{}输", player.getWxId(), zh.getWxId());
                    double score = 0;
                    if (player.isPoker()) {
                        player.setPower(1.0);
                        score = player.getBet();
                    } else if (!player.isPoker()) {
                        score =player.getBet() * player.getPower();
                    }
                    //int score = player.getBet() * player.getPower();
                    zhScore -= score;
                    player.setXianWinScore((int)score);
                    winXPlayerList.add(player);
                    //updatePlayerScore(tGame.getId(), player, resultList, score, isUpdate);
                } else { // 打平
                    logger.warn("闲家{}和庄家{}打平", player.getWxId(), zh.getWxId());
                    gameService.updatePlayerScore(tGame.getId(), player, resultList, 0, false);
                }
            }
        }
    }
}
